package com.wei.controller.web;

import com.wei.blog.common.exception.BlogException;
import com.wei.blog.common.help.UserHelp;
import com.wei.blog.common.utils.R;
import com.wei.service.ILikeService;
import com.wei.vo.LikeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/like")
public class LikeController {
    @Autowired
    private UserHelp userHelp;
    @Autowired
    private ILikeService likeService;


    @PostMapping("/clickLike")
    R clickLike(@RequestBody LikeVo likeVo, ModelMap map){
        if (userHelp.get()==null){
            throw new BlogException("用户未登录");
        }
        //点赞
        likeVo.setUserId(userHelp.get().getUid());
        likeService.clickLike(likeVo);
        //统计实体（帖子或回复）点赞数量
        Long likeCount = likeService.likeCount(likeVo);
        //查询点赞的状态
        boolean likeStatus = likeService.likeStatus(likeVo);
        //将数据返回给前端
        return R.ok().put("likeStatus",likeStatus?1:0).put("likeCount",likeCount);
    }


}
